<template>
  <div class="container" ref="container">
    <Waterfall :items="items" :column-count="3" :has-more="hasMore" @load-more="fetchMoreItems">
      <template #item="{ item }">
        <div class="custom-item">
          <img :src="item.image" :alt="item.title" />
          <h3>{{ item.title }}</h3>
          <p>{{ item.description }}</p>
          <span class="price">¥{{ item.price }}</span>
        </div>
      </template>
    </Waterfall>

    <!-- 回到顶部按钮 -->
    <BackToTop
      :threshold="100"
      width="60px"
      height="60px"
      content="Top"
      :custom-style="{ backgroundColor: '#3bb8ff', fontSize: '16px' }"
      @click="handleBackToTopClick"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import Waterfall from "@/components/Waterfall/index.vue";
import BackToTop from "@/components/BackToTop/index.vue";

const items = ref([]);
const hasMore = ref(true);
const page = ref(1);
const showBackToTop = ref(false);
const container = ref(null);

// 模拟获取数据
const fetchItems = async () => {
  const mockData = Array.from({ length: 10 }, (_, i) => ({
    id: `item-${page.value}-${i}`,
    image: `https://picsum.photos/300/200?random=${page.value}${i}`,
    title: `商品 ${page.value}-${i}`,
    description: "这是商品描述内容",
    price: (Math.random() * 100).toFixed(2),
  }));

  return new Promise((resolve) => {
    setTimeout(() => resolve(mockData), 800);
  });
};

// 初始加载
onMounted(async () => {
  items.value = await fetchItems();
});

const handleBackToTopClick = () => {
  console.log("回到顶部按钮被点击");
};

// 加载更多
const fetchMoreItems = async () => {
  page.value += 1;
  const newItems = await fetchItems();
  items.value = [...items.value, ...newItems];

  // 模拟没有更多数据的情况
  if (page.value >= 3) {
    hasMore.value = false;
  }
};
</script>

<style scoped>
.container {
  padding: 20px 20px 0 20px;
  overflow-y: auto;
  /* height: 83vh; */
  position: relative; /* 为绝对定位的按钮提供参考 */
}

.custom-item {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  padding-bottom: 12px;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  }

  img {
    width: 100%;
    height: auto;
    display: block;
  }

  h3 {
    margin: 12px 12px 8px;
    font-size: 16px;
    color: #333;
  }

  p {
    margin: 0 12px 8px;
    font-size: 14px;
    color: #666;
  }

  .price {
    display: block;
    margin: 0 12px;
    font-size: 18px;
    font-weight: bold;
    color: #f5222d;
  }
}

/* 回到顶部按钮样式 */
.back-to-top {
  position: fixed;
  bottom: 40px;
  right: 40px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #1890ff;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;

  &:hover {
    background: #40a9ff;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  }

  &:active {
    transform: translateY(0);
  }
}

/* 渐变动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
